package com.zbkj.admin.boot.jackson;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.databind.ser.std.StdSerializer;

import java.io.IOException;

/**
 * js long 支持得最大值序列化
 *
 * <p>
 * 超过 js 支持的范围，改为字符串
 * </p>
 *
 * @author L.cm
 */
public class JsLongSerializer extends StdSerializer<Long> {
	private static final long JS_MAX = 9223372036854776L;
	private static final long JS_MIN = -9223372036854776L;

	public JsLongSerializer(Class<Long> clazz) {
		super(clazz);
	}

	public JsLongSerializer(JavaType type) {
		super(type);
	}

	@Override
	public void serialize(Long value, JsonGenerator gen, SerializerProvider provider) throws IOException {
		if (value < JS_MIN || value > JS_MAX) {
			gen.writeString(value.toString());
		} else {
			gen.writeNumber(value);
		}
	}

}
